/*
 * Author: vyouzhi <vyouzhi@163.com>
 * http://www.xda.cn
 *
 * File: convey_lib.c
 * Create Date: 2011-07-13 10:41:48
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "convey_lib.h"
#include "conf_lib.h"
#include "client_lib.h"
#include "mysql_lib.h"
#include "time_lib.h"

void doconvey(long id){
	int ismaster = 0;
	char command[1024], *isrec;
	isrec = conf_get("ismaster");

	if(isrec){
		ismaster = atoi(isrec);
	}

	if(ismaster){
		sprintf(command, "./convey -t %ld &", id);
		printf("%s\n", command);
		system(command);
	}
}

void getupdate(long id){
	char *master_host;
	char *master_port;
	char command[1024];

	master_host = conf_get("master_host");
	master_port = conf_get("master_port");
	
	sprintf(command, "./convey -g %ld -h %s -p %s &", id, master_host, master_port);
	printf("%s\n", command);
	system(command);

}

void tellany(char *s, long id){
	char *ss = s;
	char *host;
	int port;
	int len = 0;
	char sxml[1024];
	char sd[1024];

	bzero(sxml, 1024);	
	sprintf(sxml, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<sql><rw>u</rw><lastid>%ld</lastid></sql>", id);	
	len = strlen(sxml);
	bzero(sd, 1024);
	sprintf(sd, "Content-Length: %d \r\n %s", len, sxml);

	host = strtok(ss, ":");
	port = atoi(strtok(NULL, ":"));

	client_init(host, port);
 	client_send(sd, strlen(sd));
	/*client_close();	*/
}

int getclient(char *chp[]){
	char *buf;
	int i = 0;
	char *p[ASIZE];
	
	buf = conf_get("client");

	p[i] = strtok(buf, ",");
	chp[i] = strdup(p[i]);
	i++;
	while(i < ASIZE && (p[i] = strtok(NULL, ","))){
		chp[i] = strdup(p[i]);
		i++;
	}
	return i;
}

void tell_all_client_update(long id){
	int ismaster = 0;
	char *cls[ASIZE];
	int m = 0, i = 0;

	ismaster = atoi(conf_get("ismaster"));
	if(ismaster){
		m = getclient(cls);	
		for(i=0; i<m; i++){
			tellany(cls[i], id);
		}
		free(cls[i]);
	}
}

void getlog(long id, char *host, int port){
	char sql[1024], *gid, *sd;
	long lastid;
	int len=0;
	char *cli_host, *cli_port, *logname;
	
	logname = get_logname();
	bzero(sql, 1024);
	sprintf(sql, "SELECT id FROM `sql_log`.`sql_log_%s` order by id desc limit 1;", logname);
	free(logname);

	if(-1 == connect_sql()) exit(-1);
	gid = simple_mysql(sql);
	if(gid == NULL) lastid = 0;
	else lastid = atol(gid);

	if(lastid && lastid >= id) {
		printf("error id convey.c 105\n");
		exit(-1);
	}

	bzero(sql, strlen(sql));

	cli_host = conf_get("server_ip");
	cli_port = conf_get("server_port");
	
	sprintf(sql, 
			"<?xml version=\"1.0\" encoding=\"utf-8\"?><sql><rw>g</rw><lastid>%ld</lastid><host>%s</host><port>%s</port></sql>",
			lastid, cli_host, cli_port);

	len = strlen(sql);
	sd = (char *)calloc(len+1024, sizeof(char));
	sprintf(sd, "Content-Length: %d \r\n %s", len, sql);

	client_init(host, port);
 	client_send(sd, strlen(sd));
	free(sd);
}

void updateing(long id, char *host, int port){
	char command[1024];

	sprintf(command, "./convey -s %ld -h %s -p %d &", id, host, port);	
	printf("%s\n", command);
	system(command);
}

void doingupdate(long id, char *host, int port){
	char sql[1024], *logname;
	char *val = NULL;
	char *ixml, *isd;
	int len = 0;
	
	logname = get_logname();
	if (-1 == connect_sql())exit(-1);

	while(val == NULL){
		++id;
		bzero(sql, 1024);
		sprintf(sql,  "SELECT `sql` FROM `sql_log`.`sql_log_%s` where id = %ld  limit 1;", logname, id);
		val = simple_mysql(sql);
	
	}
	/*
	isql = (char *)calloc(strlen(sql)+1024, sizeof(char));
	sprintf(isql, "INSERT INTO `sql_log`.`sql_log_%s` (`id`, `sql`)VALUES (%ld '%s');", logname, id, val);	
	*/
	ixml = (char *)calloc(strlen(val)+1024, sizeof(char));
	bzero(ixml, strlen(val)+1024);
	sprintf(ixml , "<?xml version=\"1.0\" encoding=\"utf-8\"?><sql><rw>i</rw><id>%ld</id><sub>%s</sub><logname>%s</logname></sql>",id, val, logname);
	
	len = strlen(ixml);
	isd = (char *)calloc(len+1024, sizeof(char));
	bzero(isd, len+1024);
	sprintf(isd, "Content-Length: %d \r\n %s", len, ixml);

	
	client_init(host, port);
 	client_send(isd, strlen(isd));

	close_mysql();
	free(logname);
	/*free(isql);*/
	free(ixml);
	free(isd);
}
/* vim: set ts=4 sw=4: */
